package com.yzc.android.network.base;

import android.content.Context;

import java.io.File;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.DiskBasedCache;
import okhttp3.OkHttpClient;

/**
 * Created by yzc on 16/4/13.
 */
public class Volley3 {
    public Volley3() {
    }

    /**
     * 创建一个客户端的请求队列
     * @param context Context
     * @param stack OkHttpStack
     * @return
     */
    public static RequestQueue newRequestQueue(Context context, IOkHttpStack stack) {
        File cacheDir = new File(context.getCacheDir(), "volley");//缓存地址：/data/data/com.yyw.volleydemo/cache/volley
        if(stack == null) {//如果请求的stack为空就建一个默认的
            stack = new OkHttp3Stack(new OkHttpClient.Builder().build());
        }
        BasicNetwork3 network1 = new BasicNetwork3(stack);
        RequestQueue queue1 = new RequestQueue(new DiskBasedCache(cacheDir), network1);
        queue1.start();
        return queue1;
    }

    /**
     * 创建一个指定请求客户端的请求队列
     * @param context 上下文
     * @param client OkHttpClient
     * @return 请求队列
     */
    public static RequestQueue newRequestQueueByClient(Context context, OkHttpClient client) {
        return newRequestQueue(context, new OkHttp3Stack(client));
    }
    /**
     * 创建一个默认请求客户端的请求队列
     * @param context 上下文
     * @return
     */
    public static RequestQueue newRequestQueue(Context context) {
        return newRequestQueue(context, null);
    }
}
